package com.overlord.im.commons.mq.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import javax.annotation.Resource;

/**
 * @ClassName AbstractRabbitSenderService
 * @Author wuwei
 * @Description
 * @Date 2019/12/20 14:59
 **/
public abstract class AbstractRabbitSenderService<T> implements MqSenderService<T> {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    protected RabbitTemplate template;

    @Override
    public void send(T data) {
        logger.info("发送消息:{}", data.toString());
        sendMq(data, bindingKey(), exchange());
    }

    @Override
    public void send(T data, String bindingKey, String exchange) {
        logger.info("发送消息:{}", data.toString());
        sendMq(data, bindingKey, exchange);
    }

    protected void sendMq(T message, String bindingKey, String exchange) {
        this.template.convertAndSend(exchange, bindingKey, message);
    }

    /**
     * bindingKey
     *
     * @return
     */
    protected abstract String bindingKey();

    /**
     * 交换机名称
     *
     * @return
     */
    protected abstract String exchange();

}
